package algorithm;

import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import util.TaiHashUtil;

import javax.swing.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * CPABE的公钥类
 */
public class CPABEPK {
    private Element g;
    private Element h;
    private Element f;
    private Element ea;
    private BigInteger order;

    public CPABEPK(Element g, Element h, Element f, Element ea,BigInteger order) {
        this.g = g;
        this.h = h;
        this.f = f;
        this.ea = ea;
        this.order=order;
    }

    public CPABEPK(){

    }

    public Element getG() {
        return g;
    }

    public Element getH() {
        return h;
    }

    public Element getF() {
        return f;
    }

    public Element getEa() {
        return ea;
    }

    public String[] toStringArray(){
        String[] array=new String[5];
        array[0] = Base64.getEncoder().encodeToString(g.toBytes());
        array[1] = Base64.getEncoder().encodeToString(h.toBytes());
        array[2] = Base64.getEncoder().encodeToString(f.toBytes());
        array[3] = Base64.getEncoder().encodeToString(ea.toBytes());
        array[4] = order.toString();
        return array;
    }

    public void fromStringArray(String[] array,Pairing pairing){
        Field G1=pairing.getG1();
        Field GT=pairing.getGT();
        byte[] temp=Base64.getDecoder().decode(array[0]);
        g=G1.newRandomElement();
        g.setFromBytes(temp);
        g=g.getImmutable();
        temp=Base64.getDecoder().decode(array[1]);
        h=G1.newRandomElement();
        h.setFromBytes(temp);
        h=h.getImmutable();
        temp=Base64.getDecoder().decode(array[2]);
        f=G1.newRandomElement();
        f.setFromBytes(temp);
        f=f.getImmutable();
        temp=Base64.getDecoder().decode(array[3]);
        ea=GT.newRandomElement();
        ea.setFromBytes(temp);
        ea=ea.getImmutable();
        order=new BigInteger(array[4]);
    }

}
